<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>订单管理</title>
    <link rel="stylesheet" type="text/css" href="../css/common.css"/>
    <link rel="stylesheet" type="text/css" href="../css/main.css"/>
    <link rel="stylesheet" type="text/css" href="../css/bookManage.css"/>
    <script type="text/javascript" src="../js/modernizr.min.js"></script>
    <script type="text/javascript" src="../js/jquery-1.11.0.min.js"></script>
    <script>
        $(function () {
            //调用公共header
            $('#left').load('left.html');
            $('#header').load('header.html');
        });
    </script>
    <script>
        //修改发货状态
        function upDeliver(isDeliver, orderId) {
            if (confirm("确认发货？")) {
                $.post("../order/upDeliver.do", {isDeliver: isDeliver, orderId: orderId}, function (data) {
                    if (data != null) {
                        alert(data.errorInfo);
                        location.reload();
                    }
                });
            }
        }

        //加载页面信息
        function loadOrderManage(start, isPay, isDeliver, username) {
            $.get("../order/getOrders.do", {
                start: start,
                isPay: isPay,
                isDeliver: isDeliver,
                username: username
            }, function (data) {
                var trs = "<tr>\n" +
                    "          <th>订单编号</th>\n" +
                    "          <th>用户名&nbsp;&nbsp;</th>\n" +
                    "          <th>收获地址</th>\n" +
                    "          <th>创建时间</th>\n" +
                    "          <th>付款时间</th>\n" +
                    "          <th>发货时间</th>\n" +
                    "          <th>发货状态</th>\n" +
                    "          <th>订单总价</th>\n" +
                    "          <th>付款状态</th>\n" +
                    "          <th>物流状态</th>\n" +
                    "          <th>售后</th>\n" +
                    "          <th>操作&nbsp;&nbsp;</th>\n" +
                    "      </tr>";
                if (data.errorType == 4) {
                    alert(data.errorInfobuyers);
                } else { //权限合格  返回不为空有数据
                    for (var i = 0; i < data.list.length; i++) { //所有查出来的书籍集合
                        //特殊数据填写  付款状态 发货状态 售后  操作
                        var fk_text = ""; //付款栏文本
                        var fh_text = ""; //发货文本
                        var sh_text = ""; //售后文本
                        var wl_text = "";//物流文本
                        var tk_btn = ""; //处理退款按钮
                        var fh_btn = ""; //发货按钮
                        var xg_btn = "<a href='updateOrder.html?orderId=" + data.list[i].orderId + "&uid=" + data.list[i].buyers.uid + "'>修改&nbsp;&nbsp;</a>";
                        if (data.list[i].isPay == 1) {//已付款
                            fk_text = "买家已付款";
                            fh_text = "待发货";
                            wl_text = "待发货";
                            sh_text = "暂无售后";
                            if (data.list[i].isDeliver == 1) { //已发货
                                fh_text = "已发货";
                                wl_text = "卖家已发货";
                                if (data.list[i].isRefund == 1) { //买家申请退款
                                    sh_text = "买家申请退款中";
                                    //只要买家申请退款就生成 同意和拒绝按钮
                                    tk_btn = "<br/><a href='javascript:;' onclick='Agree(" + data.list[i].orderId + ")'>同意退款</a>" +
                                        "<br/><a href='javascript:;' onclick='Refuse(" + data.list[i].orderId + ")'>拒绝退款</a>";
                                } else if (data.list[i].isRefund == 2) {
                                    sh_text = "退款已同意";
                                } else if (data.list[i].isRefund == 3) {
                                    sh_text = "退款请求被驳回";
                                } else if (data.list[i].isRefund == 0) { //买家没有申请退款
                                    sh_text = "暂无售后记录";
                                }
                                if (data.list[i].isReceipt == 1) {//已收货
                                    wl_text = "买家已收货";
                                    sh_text = "订单已完成";
                                    xg_btn = "";
                                }
                            } else {
                                //未发货  生成发货按钮
                                fh_btn = "<a href=\"javascript:;\" onclick='upDeliver(1," + data.list[i].orderId + ")'>发货&nbsp;&nbsp;</a>";
                                fk_text = "已付款";
                                fh_text = "未发货";
                                wl_text = "买家已付款，待卖家发货";
                                sh_text = "暂无售后";
                            }
                        } else {
                            fk_text = "未付款";
                            fh_text = "未发货";
                            wl_text = "待买家付款";
                            sh_text = "暂无售后";
                        }

                        trs += "<tr>" +
                            "       <td>" + data.list[i].orderId + "</td>\n" +
                            "       <td>" + data.list[i].buyers.username + "</td>\n" +
                            "       <td>" + data.list[i].addressId.address + "</td>\n" +
                            "       <td>" + data.list[i].createTime + "</td>\n" +
                            "       <td>" + data.list[i].payTime + "</td>\n" +
                            "       <td>" + data.list[i].deliverTime + "</td>\n" +
                            "       <td>" + fh_text + "</td>\n" +
                            "       <td>" + data.list[i].totalPrice + "</td>\n" +
                            "       <td>" + fk_text + "</td>\n" +
                            "       <td>" + wl_text + "</td>\n" +
                            "       <td>" + sh_text + "</td>\n" +
                            "       <td>" + xg_btn + tk_btn + fh_btn +
                            "       </td>\n" +
                            "</tr>";
                    }
                    //表格页
                    $("#users").html(trs);
                    //分页
                    var lis = "<li>\n" +
                        "                <a href=\"#\" aria-label=\"Previous\" onclick='loadOrderManage(" + data.prePage + ")'>\n" +
                        "                    <span aria-hidden=\"true\">&laquo;</span>\n" +
                        "                </a>\n" +
                        "            </li>";
                    if (data.prePage == 0) {
                        lis = "<li class='disabled'>\n" +
                            "                <a href=\"#\" aria-label=\"Previous\">\n" +
                            "                    <span aria-hidden=\"true\">&laquo;</span>\n" +
                            "                </a>\n" +
                            "            </li>";
                    }
                    for (var i = 1; i <= data.lastPage; i++) {
                        lis += "<li><a href=\"#\"  onclick='loadOrderManage(" + i + ")'>" + i + "</a></li>";
                    }
                    //结尾
                    if (data.nextPage == 0) { //下一页大于最后一页时  按钮禁用
                        lis += "<li class='disabled'>\n" +
                            "                <a href=\"#\" aria-label=\"Next\" >\n" +
                            "                    <span aria-hidden=\"true\">&raquo;</span>\n" +
                            "                </a>\n" +
                            "            </li>";
                    } else {
                        lis += "<li>\n" +
                            "                <a href='#' onclick='loadOrderManage(" + data.nextPage + ")' aria-label=\"Next\">\n" +
                            "                    <span aria-hidden=\"true\">&raquo;</span>\n" +
                            "                </a>\n" +
                            "            </li>";
                    }
                    //生成
                    $("#pagination").html(lis);
                    $("#tips").html("当前第" + data.pageNum + "页，共" + data.pages + "页，" + data.total + "条数据");
                }
            });
        }

        //同意退款
        function Agree(orderId) {
            $.post("../order/agree.do", {orderId: orderId}, function (data) {
                alert(data.errorInfo);
                if (data.errorType == 0) {
                    location.reload();
                }
            });
        }

        //拒绝退款
        function Refuse(orderId) {
            $.post("../order/refuse.do", {orderId: orderId}, function (data) {
                alert(data.errorInfo);
                if (data.errorType == 0) {
                    location.reload();
                }
            });
        }

        //入口函数
        $(function () {
            //填充类型
            //加载主体
            loadOrderManage(1);
            //搜索
            $("#sub").click(function () {
                var username = $("#username").val();//获取搜索框的值
                // alert(bname);
                var isPay = $("#isPay").val();
                var isDeliver = $("#isDeliver").val();
                // alert(btype);
                loadOrderManage(1, isPay, isDeliver, username);
            });

        });
    </script>
</head>
<body>
<div class="container clearfix">
    <div id="header"></div>
    <div id="left"></div>
    <!--/sidebar-->
    <div class="main-wrap">
        <div class="crumb-wrap">
            <div class="crumb-list"><i class="icon-font">&#xe014;</i><a href="javascript:;">订单管理</a>
            </div>
        </div>
        <div class="search-wrap">
            <!--nav 导航-->
            <div class="search-wrap">
                <div class="search-content">
                    <table class="search-tab">
                        <tr>
                            <th width="120">付款状态：</th>
                            <td>
                                <select name="search-sort" id="isPay">
                                    <option selected value="-1">----请选择----</option>
                                    <option value="0">未付款</option>
                                    <option value="1">已付款</option>
                                </select>
                            </td>
                            <td>
                                <select name="search-sort" id="isDeliver">
                                    <option selected value="-1">----请选择----</option>
                                    <option value="0">未发货</option>
                                    <option value="1">已发货</option>
                                </select>
                            </td>
                            <th width="90">用户名:</th>
                            <td><input class="common-text" placeholder="请输入姓名关键词..." id="username"
                                       type="text"></td>
                            <td><input class="btn btn-primary btn2" name="sub" id="sub" value="查询" type="submit"></td>
                        </tr>
                    </table>
                </div>
            </div>
            <!--正文 主体-->
            <div class="result-wrap">
                <form name="myform" id="myform" method="post">
                    <div class="result-title">
                        <div class="result-list">

                        </div>
                    </div>
                    <div class="result-content">
                        <table class="table table-hover" id="users">
                        </table>
                    </div>
                </form>
            </div>
        </div>
        <!--footer 页脚-->
        <div class="footer">
            <nav aria-label="Page navigation">
                <ul class="pagination" id="pagination">
                </ul>
            </nav>
            <p id="tips"></p>
        </div>
    </div>
</div>
</body>
</html>